package com.bkaka.controller;

import com.bkaka.bean.ResponseBean;
import com.bkaka.email.EmailService;
import com.bkaka.entity.ExamUser;
import com.bkaka.entity.ExamUserCode;
import com.bkaka.service.ExamUserCodeServer;
import com.bkaka.service.ExamUserService;
import com.bkaka.util.code.CodeUtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * Created by maxiaoqiang on 2018/7/18.
 */
@RestController
@RequestMapping(value = "${api.path}")
public class ExamUserController {

    @Autowired
    private ExamUserService service;

    @Autowired
    private ExamUserCodeServer codeServer;

    @ResponseBody
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    public ResponseBean login(@Valid @RequestBody ExamUser examUser) {
        ExamUser user = service.login(examUser);
        if (user != null) {
            return new ResponseBean(200, "SUCCESS", user);
        } else {
            return new ResponseBean(500, "用户名或密码错误", null);
        }
    }

    @ResponseBody
    @PostMapping(value = "/register")
    public ResponseBean register(@Valid @RequestBody ExamUser examUser) {
        // 查询是否注册过
        ExamUser user = service.findByUserName(examUser.getUserName());
        if (user == null) {
            // TODO 判断验证码是否正确
            ExamUserCode userCode = codeServer.findByNameCode(examUser.getUserName());
            // 没有获取验证码
            if (userCode == null) {
                return new ResponseBean(500, "验证码错误", examUser);
            }
            // 判断验证是否过期
            Long outTime = userCode.getOutTime();
            Long createTime = userCode.getCreateTime();
            Long time = new Date().getTime();
            if (time >= outTime * 1000 + createTime) {// 验证码过期
                return new ResponseBean(500, "验证码过期，请重新获取", examUser);
            } else {
                if (userCode.getCode().equals(examUser.getCode())) { // 验证码正确
                    // 新建用户
                    ExamUser newUser = new ExamUser();
                    newUser.setUserName(examUser.getUserName());
                    newUser.setPassword(examUser.getPassword());
                    service.create(newUser);
                    // 删除验证码
                    codeServer.delete(userCode.getId());
                    return new ResponseBean(200, "注册成功", newUser);
                } else {
                    return new ResponseBean(500, "验证码错误", examUser);
                }
            }
        } else {
            return new ResponseBean(500, "用户已存在", examUser);
        }
    }

    @ResponseBody
    @PostMapping(value = "/update")
    public ResponseBean update(@Valid @RequestBody ExamUser examUser) {
        service.update(examUser);
        ExamUser user = service.findById(examUser.getId());
        return new ResponseBean(200, "success", user);
    }

    @ResponseBody
    @PostMapping(value = "/forget")
    public ResponseBean forget(@Valid @RequestBody ExamUser examUser) {
        return null;
    }


}
